package com.ruike.alisurface.ui.answerQuestion;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.ui.answerQuestion.bean.ResultAnswerBean;
import com.ruike.alisurface.ui.payPage.PayShowShopActivity;
import com.ruike.alisurface.utils.GlideImageLoader;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.FileUtils;
import com.voodoo.lib_utils.ShareUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.ruike.alisurface.Constant.backgroundImg;

/**
 * Author: voodoo
 * CreateDate: 2023/11/21 021下午 01:12
 * Description: 展示答题结果界面
 */
public class QuestionResultActivity extends BaseAty {

    @BindView(R.id.answerResult_bg_imgv)
    ImageView backgroundImgv; // 背景图
    @BindView(R.id.answerResult_number_tv)
    TextView numberTv; // 一共多少道题，答对多少道答错多少道 （您共答 4 题，正确 3 题，错误 1 题）
    @BindView(R.id.answerResult_result_tv)
    TextView resultTv; // 结果展示 （未/达到领取要求）
    @BindView(R.id.answerResult_next_tv)
    TextView nextTv; // 按钮 （领取商品/错题详情）

    boolean isOutGoods = false; // 是否可以出货（是否达到出货要求）
    ResultAnswerBean resultAnswerBean;

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_question_result;
    }

    @Override
    protected void initViews() {
        startCountdownTimer(30, null, null);
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            resultAnswerBean = (ResultAnswerBean) bundle.getSerializable("resultAnswerBean");
        }

        if (resultAnswerBean != null) {

            isOutGoods = resultAnswerBean.getData().isCan_out();

            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("您共答 ")
                    .append(resultAnswerBean.getData().getTotal_question())
                    .append(" 题，正确 ")
                    .append(resultAnswerBean.getData().getSuccess_question())
                    .append(" 题，错误 ")
                    .append(resultAnswerBean.getData().getError_question())
                    .append(" 题");
            numberTv.setText(stringBuffer.toString());
            resultTv.setText(isOutGoods ? "达到出货要求" : "未达到出货要求");
            nextTv.setText(isOutGoods ? "领取商品" : "返回主界面");
        }


    }

    @Override
    protected void onStart() {
        super.onStart();
        File[] files = FileUtils.getFolderFileList(backgroundImg);
        if (files != null && files.length > 0) {
            String bgPath = files[0].getAbsolutePath();
            GlideImageLoader.loadImage(this, bgPath, backgroundImgv);
        }
    }

    @OnClick({R.id.answerResult_next_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.answerResult_next_tv:
                if (isOutGoods) {

                    ShopDetailBean shopDetailBean = new ShopDetailBean(resultAnswerBean.getData().getOrder_id());
                    shopDetailBean.setTotal_fee(0);
                    shopDetailBean.setPay_fee(0);
                    shopDetailBean.setPromotion_fee(0);
                    shopDetailBean.setDiscount_fee(0);

                    List<ShopDetailBean.OrderDetailsBean> shopOrderDetailsBeanList = new ArrayList<>();
                    shopOrderDetailsBeanList.clear();

                    ShopDetailBean.OrderDetailsBean shopOrderDetailsBean = new ShopDetailBean.OrderDetailsBean();
                    shopOrderDetailsBean.setSlotTypeId(resultAnswerBean.getData().getSlotTypeId()); // 货道类型
                    shopOrderDetailsBean.setSlotIndex(resultAnswerBean.getData().getOut_slot()); // 出货货道编号
                    shopOrderDetailsBean.setQuantity(1); // 出货数量
                    shopOrderDetailsBeanList.add(shopOrderDetailsBean);

                    shopDetailBean.setDetailOrderList(shopOrderDetailsBeanList);

                    Bundle bundle = new Bundle();
                    bundle.putString("pay_type", "ansertQuestion");
                    bundle.putSerializable("orderBeanData", shopDetailBean);
                    startActivity(PayShowShopActivity.class, bundle);
                    finish();
                } else {
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("resultAnswer", resultAnswerBean);
//                    startActivity(AnswerErrorInfoActivity.class, bundle);
//                    finish();
                    startCountdownTimer(0, null, null);
                }
                break;
        }
    }
}
